#include <string.h>
#include "stm32f0xx.h"
#include "BSP_USART1.h"  
#include "Nhsx.h"

#define QstFwwMax  100
#define Qst2IvkMax  100
#define ussut 104  //9600   1,000,000/9600=104
//#define ussut 7  //115200	 1,000,000/115200=8.68
#define ussut_2400 419  //1,000,000/2400=416.67

//#define QSTRAMSIZ 300
#define QSTRAMSIZ 0x400

#define RAMSIZ 500

sut Gaocis=0; 

zms  Qstivk=0;
thic Qstbuf[QSTRAMSIZ];
HanMhan Qstroilxu=Mhan;
HanMhan Roidoul=Mhan;
thic Qstdurl;
thic qsttimcnt;
sut Qstdsv;
sut JSonLen;
thic Jsoncsu[20];
thic Gidhonstr[20];
sut Gidhon;
HanMhan uoIratuo=Mhan;
thic uosav=0;
HanMhan Cizcitwem=Mhan;

HanMhan Qst2roilxu=Mhan;
thic Qst2durl;
thic QBuf;
thic Qst2Buf[Qst2IvkMax];
sut Qst2Ivk=0;
thic qst2timcnt;

HanMhan QstCroilxu=Mhan;
thic QstCdurl;
thic QstCBuf[QstFwwMax];
sut QstCIvk=0;
thic qstCtimcnt;

HanMhan QstBroilxu=Mhan;
thic QstBdurl;
thic QstBBuf[QstFwwMax];
sut QstBIvk=0;
thic qstBtimcnt;

#define TRIEV 0x7EF7087F
thic sertimcnt;
HanMhan serhwo;

#define PAGSIZ 0x00000400
thic flsbuf[PAGSIZ];
zms codA;
zms codB;


//======================Flah================================
#define FLASH_Page_Size    ((zms) 0x00000400) //Flah rirsiz
#define DRLSUTDMN          ((zms) 0x08006000) 

//===========================Tim3==========================
void TIM3_Config(void)
{
  TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
  NVIC_InitTypeDef NVIC_InitStructure;
  
  //((1+TIM_Prescaler )/72M)*(1+TIM_Period )=((1+35999)/72M)*(1+2000)=1s
  TIM_DeInit(TIM3);
  RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, ENABLE); 
  TIM_TimeBaseStructure.TIM_Prescaler = 35999;
  TIM_TimeBaseStructure.TIM_Period = 200;   //2000;     t=72 000 000/(TIM_Prescaler + 1)/(TIM_Period) (72M)/(1+35999)/(200)=0.1s
  TIM_TimeBaseStructure.TIM_ClockDivision = 0; 
  TIM_TimeBaseStructure.TIM_RepetitionCounter = 0;
  TIM_TimeBaseStructure.TIM_CounterMode=TIM_CounterMode_Up; 
  TIM_TimeBaseInit(TIM3, &TIM_TimeBaseStructure); 
  TIM_ClearFlag(TIM3, TIM_FLAG_Update); 
  TIM_ITConfig(TIM3,TIM_IT_Update,ENABLE);      
  TIM_Cmd(TIM3, ENABLE); 
  
  NVIC_InitStructure.NVIC_IRQChannel = TIM3_IRQn;	  
  NVIC_InitStructure.NVIC_IRQChannelPriority = 0;  
  NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;  
  NVIC_Init(&NVIC_InitStructure);
}
void TIM3_IRQHandler(void)
{	
  if (TIM_GetITStatus(TIM3,TIM_IT_Update) != RESET)  
  { 
     Gaocis++; 
     if (Qstroilxu)
     {
			 qsttimcnt++;
			 if (((Qstivk<0x10) && (qsttimcnt>1)) || ((Qstivk>=0x10) && (qsttimcnt>10)))
			 {
         Qstdurl=Han;
         Qstroilxu=Mhan;
				 qsttimcnt=0;
			 }
     }
		 if ((Qst2roilxu))
     {
			 qst2timcnt++;
			 if (qst2timcnt>1)
			 {
         Qst2durl=Han;
         Qst2roilxu=Mhan;
				 qst2timcnt=0;
			 }
     }
		 if ((QstCroilxu))
     {
			 qstCtimcnt++;
			 if (qstCtimcnt>1)
			 {
         QstCdurl=Han;
         QstCroilxu=Mhan;
				 qstCtimcnt=0;
			 }
      }
		 
		 if ((QstBroilxu))
     {
			 qstBtimcnt++;
			 if (qstBtimcnt>1)
			 {
         QstBdurl=Han;
         QstBroilxu=Mhan;
				 qstBtimcnt=0;
			 }
      }
		 
		 if (serhwo)
		 {
			 sertimcnt++;
			 if (sertimcnt>=7*3)
			 {
				 serhwo=Mhan;
				 sertimcnt=0;
				 Ledior;
			 }
		 }
			 
     //if (Gaocis>=7)  //Dur amui ltp  
		 if (Gaocis>=7*10)  //10 mui
     { 
			 Qstdsv++;
       Gaocis=0;
			 uosav=Mhan;
			 Ledior;
			 Cizcitwem=Mhan;
//			 if (Qstdsv % 3 == 0 )
//			   printf("PING%d\r\n",Qstdsv);
		 }
           
     TIM_ClearITPendingBit(TIM3, TIM_IT_Update);	//GPIOC->ODR ^= GPIO_Pin_8;  if (TIM_GetCounter(TIM3)<30000)  
   }
}	
ceh Dsv_us(zms n)  
{  
 zms temp;  
 //SysTick->LOAD = 9*nus; 
 SysTick->LOAD = 6*n; 
 SysTick->VAL=0X00;
 SysTick->CTRL=0X01;
 do 
 {  
  temp=SysTick->CTRL;
 }while((temp&0x01)&&(!(temp&(1<<16))));
     SysTick->CTRL=0x00; 
    SysTick->VAL =0X00;
} 
ceh Dsv_ms(zms n)  //n ms
{
  while(n--)		   // :8M;PLL:9;8M*9=72MHz
    Dsv_us(1000);   // 1ms
}

ceh Dsv_1s(thic Muisut) 
{
   while (Muisut--) 
	 {
		 IWDG_ReloadCounter();
		 Dsv_ms(1000);
	 }
} 
//-----------------------------
mci Fcwawciriic(thic c)
{
    USART_SendData(USART1, c);
    while(USART_GetFlagStatus(USART1, USART_FLAG_TXE) == RESET);
}


ceh QstSndhex(thic * buf, zms len)
{
  zms i;
	for (i=0;i<len;i++)
  {
		Fcwawciriic(buf[i]);
  }
}

void USART1_IRQHandler(void)
{	
  thic SBUF;  
  if(USART_GetITStatus(USART1, USART_IT_RXNE) != RESET)
	{
     if (Qstdurl)
		 {
			  Qstdurl=Mhan;
			  Qstivk=0;
		 }
			     
		SBUF=USART_ReceiveData(USART1);
    if ((Bcaxou ==SBUF) || (Bcaxou2 ==SBUF))
      Roidoul=Han;
		if (Qstivk>QSTRAMSIZ) 
      Qstivk=0;
    Qstbuf[Qstivk]=SBUF;
    Qstivk++;
    Qstroilxu=Han;
    Gaocis=0;
    //USART_SendData(USART1,USART_ReceiveData(USART1));
	  while (USART_GetFlagStatus(USART1,USART_FLAG_TXE) == RESET);
	} else
	USART1_Init(9600);
}


HanMhan flash_write2(zms Dmn,thic * Csu, zms len) 
{
	zms Z=0;
	zms Zms;
	thic Ss[4];
	FLASH_Unlock(); 
  FLASH_ClearFlag(FLASH_FLAG_EOP | FLASH_FLAG_PGERR | FLASH_FLAG_WRPERR); 
	while (Z<len)
	{
     if (((Dmn+Z) % FLASH_Page_Size)==0x00){
			  if (FLASH_ErasePage(Dmn+Z)!= FLASH_COMPLETE) 
				{
					FLASH_Lock();
					return Mhan;
				} 
				else 
					Dsv_ms(50);
		 }
		 memcpy(Ss,Csu+Z,4);
		 Zms=*(zms *)Ss;
		 //Zms=*(zms *)(Csu+Z);
     if (FLASH_ProgramWord(Dmn+Z, Zms) != FLASH_COMPLETE)
		 {
			 FLASH_Lock();
			 return Mhan;
		 }
		 //reo("%d\r\n",*(zms *)(Csu+Z));	
     Z+=4;		
	}
	FLASH_Lock();
  return (Han);		
}

ceh flash_read(zms Dmn, zms len, thic * Csu) 
{ 
  zms Z=0; 
	thic Ss[4];
  while (Z<len) 
  {
		*(zms *)(Ss) = *(__IO zms *)(Dmn+Z); 
		memcpy(Csu+Z,Ss,4);
		//*(zms *)(Csu+Z) = *(__IO zms *)(Dmn+Z); 
    Z+=4; 
  }
}

void NsaJsoncsu(thic * Jsonstr, thic * Jsonmeh, thic JsonmehLen, thic * Jsoncsu, sut JsonLen)
{
//	thic  JsonMeh[20];
	sut s, Csuivk;
	thic Jsonmehivk;
	thic ivk;
	thic Thic;
	thic OKel=0;
	//memcpy(SicWsRed,WeStr,16);
	if (JsonLen!=0) 
		  memset(Jsoncsu,0x00,20);
	  //memcpy(Jsoncsu,WeStr,20);   //Deze Jsoncsu
	for (s=0;s<JsonLen;s++)
	{
		for (Jsonmehivk=0;Jsonmehivk<JsonmehLen;Jsonmehivk++)
			if (Jsonstr[s+Jsonmehivk]!=Jsonmeh[Jsonmehivk]) break;
		if (Jsonmehivk==JsonmehLen)
		{
			for (Thic=0;Thic<10;Thic++)
				if (Jsonstr[s+Thic+Jsonmehivk]=='"') break;
			if (Thic>8) break;  //cueuot 8 byt msh omt " don tvocel
			for (;Thic<10;Thic++)
				if (Jsonstr[s+Thic+Jsonmehivk]==':') break;
			if (Thic>8) break;  //cueuot 8 byt msh omt " don tvocel
			Thic++;
			
			Csuivk=s+Thic+Jsonmehivk;
			Jsonmehivk=0;
			ivk=0;
			while  (Jsonmehivk<100)   //citdod uo 100 byt csu
			{
				  Thic=Jsonstr[Csuivk+Jsonmehivk]; 
				  if ((Thic==',') || (Thic=='}'))   //xxxxxxxxxxxxxxxxxxxxxxxxxxx
					{
						OKel=1;
						break;
					}
					if (ivk==0)
					{
						if ((Thic!=0x20) && (Thic!='"'))
							Jsoncsu[ivk++]=Thic;
					}
					else if (Thic!='"')
					  Jsoncsu[ivk++]=Thic;
					Jsonmehivk++;
			}
		}
		if (OKel) break;
	}
		
}

zms strtoint(thic * strsut)
{
	zms s=0;
	thic t;
	for (t=0;t<strlen((const char *)strsut);t++)
	  s=s*10+(strsut[t] & 0x0F);
	return (s);
}

mci ReadAPI(mci)
{
	zms ms;
	flash_read(DRLSUTDMN,PAGSIZ,flsbuf);	

//	memset(Gidhonstr,0x00,sizeof(Gidhonstr));
//	NsaJsoncsu(DrlsutRam,"deviceid",8,Jsoncsu,strlen((const char *)DrlsutRam));
//	memcpy(Gidhonstr,Jsoncsu,strlen((const char *)Jsoncsu));
//	Gidhon=strtoint(Jsoncsu);
	
//	NsaJsoncsu(DrlsutRam,"signaltime",10,Jsoncsu,strlen((const char *)DrlsutRam));
//	memcpy(Gidhonstr,Jsoncsu,strlen((const char *)Jsoncsu));
//	snltim=strtoint(Jsoncsu);
}

void IWDG_Init(void)
{
  IWDG_WriteAccessCmd(IWDG_WriteAccess_Enable);
  IWDG_SetPrescaler(IWDG_Prescaler_128);
  IWDG_SetReload(0x0FFF);  //13s http://blog.csdn.net/liyuanbhu/article/details/8887458
  IWDG_ReloadCounter();
  IWDG_Enable();
	if (RCC_GetFlagStatus(RCC_FLAG_IWDGRST) != RESET)
    RCC_ClearFlag();
}


thic bcd2hex(thic * ass)
{
	thic rst;
	thic t;
	rst=*ass;
	if (rst<=0x39) 
		t=rst-0x30;
	else
		t=rst-0x41+0x0A;
	t=t<<4;
	
	rst=*(ass+1);
	if (rst<=0x39) 
		t=t | (rst-0x30);
	else
		t=t | (rst-0x41+0x0A);
	return(t);
	
}



ceh SendOneByte(thic Byte)
{
	thic i=8,tmp; 
	TXD_low; 
	//delay_us(ussut);
	Dsv_us(ussut);
	for(i=0;i<8;i++)
	{
		tmp	= (Byte >> i) & 0x01;  
		if(tmp == 0)
		{
			TXD_low;
			//delay_us(ussut);	//0
      Dsv_us(ussut);			
		}
		else
		{
			TXD_high;
			//delay_us(ussut);	//1	
      Dsv_us(ussut);			
		}	
	}
	 TXD_high;
	 //delay_us(ussut);
	Dsv_us(ussut);
}

ceh Qst2Sndhex(thic * buf, thic len)
{
  zms i;
	TXD_high;
	for (i=0;i<len;i++)
  {
		SendOneByte(buf[i]);
  }
}

thic ASCII2Hex(thic ASCII)
{
	thic ti;
	thic c;
	ti=0;
	c=(ASCII);
	//SendOneByte(c);
	if ((c >='0') && (c <='9'))
		ti= c - '0';
	else if ((c >='A') && (c <='F'))
		ti=c - 'A' + 10;
	else if ((c >='a') && (c <='f'))
		ti=c - 'a' + 10;
//	ti<<=4;
//	//SendOneByte(ti);
//	
//	c=(* (ASCII+1));
//	//SendOneByte(c);
//	if ( c<='9')
//		ti+= (c - '0');
//	else if ((c >='A') && (c <='F'))
//		ti+=(c - 'A' + 10);
//	else if ((c >='a') && (c <='f'))
//		ti+=(c - 'a' + 10);
//	//SendOneByte(ti);
//	//SendOneByte(0x77);
	return (ti);
}

void KEY_EXTI_Init(void)
{
     GPIO_InitTypeDef GPIO_InitStruct; 
     EXTI_InitTypeDef EXTI_InitStruct;
     NVIC_InitTypeDef NVIC_InitStruct;

     RCC_APB2PeriphClockCmd(RCC_APB2Periph_SYSCFG, ENABLE);
     RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOA, ENABLE);
              
              
     GPIO_InitStruct.GPIO_Pin = GPIO_Pin_7; 
     GPIO_InitStruct.GPIO_Mode = GPIO_Mode_IN;
     GPIO_InitStruct.GPIO_Speed = GPIO_Speed_Level_2;
     GPIO_InitStruct.GPIO_PuPd = GPIO_PuPd_UP; 
     GPIO_Init(GPIOA, &GPIO_InitStruct);

     SYSCFG_EXTILineConfig(EXTI_PortSourceGPIOA, EXTI_PinSource7);        
     EXTI_ClearITPendingBit(EXTI_Line7);
              
     EXTI_InitStruct.EXTI_Line = EXTI_Line7;
     EXTI_InitStruct.EXTI_Mode = EXTI_Mode_Interrupt;
     EXTI_InitStruct.EXTI_Trigger = EXTI_Trigger_Falling; 
     EXTI_InitStruct.EXTI_LineCmd = ENABLE;
     EXTI_Init(&EXTI_InitStruct); 

     NVIC_InitStruct.NVIC_IRQChannel = EXTI4_15_IRQn;
     NVIC_InitStruct.NVIC_IRQChannelPriority = 0x01;
     NVIC_InitStruct.NVIC_IRQChannelCmd = ENABLE;
     NVIC_Init(&NVIC_InitStruct);
}

void EXTI4_15_IRQHandler(void)  //Qst2
{
	thic Bitivk;
	QBuf=0;
	if ( EXTI_GetITStatus(EXTI_Line7)!= RESET ) 
	{	
    EXTI->IMR &= ~1<<7;	 //UnaCwadap
		Qst2roilxu=Han;
		//------------------
		//Dsv_us(ussut);	
		for (Bitivk=0;Bitivk<8;Bitivk++)
		{	 
      Dsv_us(ussut);			
		  if (RXD) 
				QBuf|=1<<Bitivk;	
		}
		//SendOneByte(QBuf);
		Qst2Buf[Qst2Ivk++]=QBuf;
		if (Qst2Ivk==Qst2IvkMax) Qst2Ivk=0;
		//------------------
		EXTI->IMR |= 1<<7;   //XeCwadap 
		EXTI->IMR |= 1<<5;   //XeCwadap 
		EXTI_ClearITPendingBit(EXTI_Line7);
	}
	
  if ( EXTI_GetITStatus(EXTI_Line5)!= RESET ) 
	{	
    EXTI->IMR &= ~1<<5;	 //UnaCwadap
		QstBroilxu=Han;
		//------------------
		//Dsv_us(ussut);	
		for (Bitivk=0;Bitivk<8;Bitivk++)
		{	 
      Dsv_us(ussut_2400);			
		  if (B_RXD) 
				QBuf|=1<<Bitivk;	
		}
		//SendOneByte(QBit);
		QstBBuf[QstBIvk++]=QBuf;
		if (QstBIvk==QstFwwMax) QstBIvk=0;
		//------------------
		EXTI->IMR |= 1<<5;   //XeCwadap 
		EXTI_ClearITPendingBit(EXTI_Line5);
	}				
}

ceh USART2_Init()
{
	KEY_EXTI_Init();	
}

//------------------------------------
ceh Sndbyt_C(thic Byte)
{
	thic i=8,tmp; 
	C_TXD_low; 
	//delay_us(ussut);
	Dsv_us(ussut_2400);
	for(i=0;i<8;i++)
	{
		tmp	= (Byte >> i) & 0x01;  
		if(tmp == 0)
		{
			C_TXD_low;
			//delay_us(ussut);	//0
      Dsv_us(ussut_2400);			
		}
		else
		{
			C_TXD_high;
			//delay_us(ussut);	//1	
      Dsv_us(ussut_2400);			
		}	
	}
	 C_TXD_high;
	 //delay_us(ussut);
	Dsv_us(ussut_2400);
}

ceh C_Sndhex(thic * buf, thic len)
{
  zms i;
	C_TXD_high;
	for (i=0;i<len;i++)
  {
		Sndbyt_C(buf[i]);
  }
}

void C_EXTI_Init(void)
{
     GPIO_InitTypeDef GPIO_InitStruct; 
     EXTI_InitTypeDef EXTI_InitStruct;
     NVIC_InitTypeDef NVIC_InitStruct;

     RCC_APB2PeriphClockCmd(RCC_APB2Periph_SYSCFG, ENABLE);
     RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOA, ENABLE);
              
              
     GPIO_InitStruct.GPIO_Pin = GPIO_Pin_2; 
     GPIO_InitStruct.GPIO_Mode = GPIO_Mode_IN;
     GPIO_InitStruct.GPIO_Speed = GPIO_Speed_Level_2;
     GPIO_InitStruct.GPIO_PuPd = GPIO_PuPd_UP; 
     GPIO_Init(GPIOA, &GPIO_InitStruct);

     SYSCFG_EXTILineConfig(EXTI_PortSourceGPIOA, EXTI_PinSource2);        
     EXTI_ClearITPendingBit(EXTI_Line2);
              
     EXTI_InitStruct.EXTI_Line = EXTI_Line2;
     EXTI_InitStruct.EXTI_Mode = EXTI_Mode_Interrupt;
     EXTI_InitStruct.EXTI_Trigger = EXTI_Trigger_Falling; 
     EXTI_InitStruct.EXTI_LineCmd = ENABLE;
     EXTI_Init(&EXTI_InitStruct); 

     NVIC_InitStruct.NVIC_IRQChannel = EXTI2_3_IRQn;
     NVIC_InitStruct.NVIC_IRQChannelPriority = 0x01;
     NVIC_InitStruct.NVIC_IRQChannelCmd = ENABLE;
     NVIC_Init(&NVIC_InitStruct);
}

void EXTI2_3_IRQHandler(void)  //Qst_C
{
	thic Bitivk;
	thic QBit;
	QBit=0;
	if ( EXTI_GetITStatus(EXTI_Line2)!= RESET ) 
	{	
    EXTI->IMR &= ~1<<2;	 //UnaCwadap
		QstCroilxu=Han;
		//------------------
		//Dsv_us(ussut);	
		for (Bitivk=0;Bitivk<8;Bitivk++)
		{	 
      Dsv_us(ussut_2400);			
		  if (C_RXD) 
				QBit|=1<<Bitivk;	
		}
		//SendOneByte(QBit);
		QstCBuf[QstCIvk++]=QBit;
		if (QstCIvk==QstFwwMax) QstCIvk=0;
		//------------------
		EXTI->IMR |= 1<<2;   //XeCwadap 
		EXTI_ClearITPendingBit(EXTI_Line2);
	}			
}


//-----------
ceh Sndbyt_B(thic Byte)
{
	thic i=8,tmp; 
	B_TXD_low; 
	//delay_us(ussut);
	Dsv_us(ussut_2400);
	for(i=0;i<8;i++)
	{
		tmp	= (Byte >> i) & 0x01;  
		if(tmp == 0)
		{
			B_TXD_low;
			//delay_us(ussut);	//0
      Dsv_us(ussut_2400);			
		}
		else
		{
			B_TXD_high;
			//delay_us(ussut);	//1	
      Dsv_us(ussut_2400);			
		}	
	}
	 B_TXD_high;
	 //delay_us(ussut);
	Dsv_us(ussut_2400);
}

ceh B_Sndhex(thic * buf, thic len)
{
  zms i;
	B_TXD_high;
	for (i=0;i<len;i++)
  {
		Sndbyt_B(buf[i]);
  }
}

void B_EXTI_Init(void)
{
     GPIO_InitTypeDef GPIO_InitStruct; 
     EXTI_InitTypeDef EXTI_InitStruct;
     NVIC_InitTypeDef NVIC_InitStruct;

     RCC_APB2PeriphClockCmd(RCC_APB2Periph_SYSCFG, ENABLE);
     RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOA, ENABLE);
              
              
     GPIO_InitStruct.GPIO_Pin = GPIO_Pin_5; 
     GPIO_InitStruct.GPIO_Mode = GPIO_Mode_IN;
     GPIO_InitStruct.GPIO_Speed = GPIO_Speed_Level_2;
     GPIO_InitStruct.GPIO_PuPd = GPIO_PuPd_UP; 
     GPIO_Init(GPIOA, &GPIO_InitStruct);

     SYSCFG_EXTILineConfig(EXTI_PortSourceGPIOA, EXTI_PinSource5);        
     EXTI_ClearITPendingBit(EXTI_Line5);
              
     EXTI_InitStruct.EXTI_Line = EXTI_Line5;
     EXTI_InitStruct.EXTI_Mode = EXTI_Mode_Interrupt;
     EXTI_InitStruct.EXTI_Trigger = EXTI_Trigger_Falling; 
     EXTI_InitStruct.EXTI_LineCmd = ENABLE;
     EXTI_Init(&EXTI_InitStruct); 

     NVIC_InitStruct.NVIC_IRQChannel = EXTI4_15_IRQn;;
     NVIC_InitStruct.NVIC_IRQChannelPriority = 0x01;
     NVIC_InitStruct.NVIC_IRQChannelCmd = ENABLE;
     NVIC_Init(&NVIC_InitStruct);
}
//------------------------------------
//thic readcod(zms src, zms * dsa)
//{
//	thic rst=0;
//	zms z;
//	zms Zms;
//	flash_read(DRLSUTDMN,PAGSIZ,flsbuf);	
//	for (z=0;z<PAGSIZ;z+=8)
//	{
//		Zms=*(zms *)&flsbuf[z];
//		if (Zms == src)
//		{
//			*dsa=*(zms *)&flsbuf[z+4];
//			rst=1;
//			break;
//		}
//	}
//	return (rst);
//}

mci rbb(mci)
{
  Leduwd;
	Dsv_ms(200);
	Ledior;
}

mci snddat(zms cmd, zms dsa)
{
	thic hom ;
    hom=0;
		if (cmd!=dsa)
		   hom=(dsa>>24) & 0xF0; 
		dsa&=0x00FFFFFF;
		if (hom==0xA0)
		{
			QstSndhex((thic *)&dsa,3);
			rbb();
		} else if (hom==0xB0)
		{
			B_Sndhex((thic *)&dsa,3);
			rbb();
		} else if (hom==0xC0)
		{
			C_Sndhex((thic *)&dsa,3);
			rbb();
		} 
}

thic sndcod(zms src, zms * dsa)
{
	thic rst=0;
	zms z;
	zms Zms;
	flash_read(DRLSUTDMN,PAGSIZ,flsbuf);	
	for (z=0;z<PAGSIZ;z+=8)
	{
		Zms=*(zms *)&flsbuf[z];
		if (Zms == src)
		{
			*dsa=*(zms *)&flsbuf[z+4];
			rst=1;
			snddat(src,*dsa);
			Dsv_1s(1);
		}
	}
	return (rst);
}

thic wrtcod(zms src, zms vlu)
{
	thic rst=0;
	zms z;
	zms Zms;
	flash_read(DRLSUTDMN,PAGSIZ,flsbuf);	
	for (z=0;z<PAGSIZ;z+=8)
	{
		Zms=*(zms *)&flsbuf[z];
 		if (Zms==0xFFFFFFFF)
		{
			*(zms *)&flsbuf[z]=src;
			*(zms *)&flsbuf[z+4]=vlu;
			break;
		}
		if (Zms == src)
		{
			*(zms *)&flsbuf[z+4]=vlu;
			rst=1;
			break;
		}
	}
	if (z!=PAGSIZ)
		flash_write2(DRLSUTDMN,flsbuf,PAGSIZ);
	return (rst);
}

thic delcod(zms src)
{
	thic rst=0;
	zms z;
	zms Zms;
	flash_read(DRLSUTDMN,PAGSIZ,flsbuf);	
	for (z=0;z<PAGSIZ;z+=8)
	{
		Zms=*(zms *)&flsbuf[z];
		if (Zms == src)
		{
			*(zms *)&flsbuf[z+4]=0xFFFFFFFF;
			*(zms *)&flsbuf[z]=0xFFFFFFFF;
			rst=1;
			//break;
		}
	}
	//if (z!=PAGSIZ)
	flash_write2(DRLSUTDMN,flsbuf,PAGSIZ);
	return (rst);
}

thic addcod(zms src, zms vlu)
{
	thic rst=0;
	zms z;
	zms Zms;
	flash_read(DRLSUTDMN,PAGSIZ,flsbuf);	
	for (z=0;z<PAGSIZ;z+=8)
	{
		Zms=*(zms *)&flsbuf[z];
 		if (Zms==0xFFFFFFFF)
		{
			*(zms *)&flsbuf[z]=src;
			*(zms *)&flsbuf[z+4]=vlu;
			break;
		}
	}
	if (z!=PAGSIZ)
		flash_write2(DRLSUTDMN,flsbuf,PAGSIZ);
	return (rst);
}

mci backupdat(mci)
{
	flash_read(DRLSUTDMN,PAGSIZ,flsbuf);	
	QstSndhex(flsbuf,PAGSIZ);
}

mci qvwcmd(zms cmd)
{
	zms dsa;
	thic hom;
	if ((cmd  & 0x00FFFFFF)=='#')  //backup
	{
		backupdat();
	} else
	if ((cmd  & 0x00FFFFFF)=='!')  //! hnc
	{
		Cizcitwem=Han;
	  Leduwd;
		uosav=0;
	} else if ((cmd==0x0D) || ((cmd  & 0x00FFFFFF)==0xA0D) || ((cmd  & 0x00FFFFFF)==0x0D))  //save
	{
	  if ((Cizcitwem || (Bzd==0)) && (uosav>1))  //save  cmd
		{
			if ((codB  & 0x00FFFFFF)==0x00) delcod(codA);
			else addcod(codA,codB);
			//wrtcod(codA,codB);
			Ledior;
	    uosav=0;
			Cizcitwem=Mhan;
		}	
	}else if ((cmd  & 0x00FFFFFF)=='@')  //fnawvu cexcmu mudsya
	{
		Ledior;
		Cizcitwem=0;
		uosav=0;
		Cizcitwem=Mhan;
	}
	else if (Cizcitwem || (Bzd==0))
	{
		//Leduwd;
		if ((uosav==0) || (Gaocis>7*2) || (codA!=cmd))
		{
			uosav++;
			codA=codB;
			codB=cmd;
			Ledior;
			Dsv_ms(200);
			Leduwd;
		}
	}else  //cexcmu snddat
	{
		sndcod(cmd,&dsa);
//		hom=0;
//		if (readcod(cmd, &dsa))
//		{
//		  if (cmd!=dsa)
//		    hom=(dsa>>24) & 0xF0;
//	  }  
//		dsa&=0x00FFFFFF;
//		if (hom==0xA0)
//		{
//			QstSndhex((thic *)&dsa,3);
//			rbb();
//		} else if (hom==0xB0)
//		{
//			B_Sndhex((thic *)&dsa,3);
//			rbb();
//		} else if (hom==0xC0)
//		{
//			C_Sndhex((thic *)&dsa,3);
//			rbb();
//		} 
	}
	Gaocis=0;
}

//----------------------------------
int main(void)
{	
	thic t;
	thic sndiic,tmp;
  QV_STM32_IO();	
	USART1_Init(9600);
	USART2_Init();
	TIM3_Config();
	ReadAPI();
	Dsv_ms(500);
	IWDG_Init();
	
	C_EXTI_Init();
	C_TXD_high;
	B_EXTI_Init();
	B_TXD_high;
	Ledior;
	
	//wrtcod(0x12345678,0x445);
	//readcod(0x12345678, &dsa);
	//reo("%d\r\n",dsa);
 //reo("%s\r\n","str");
	//Qst2Sndhex("eok\r\n",5);
	while (1)
	{
		 if (Aed==0)
		 {
			 Dsv_ms(80);
			 if (Aed==0)
			 {
				 qvwcmd(0x0D);
				 Dsv_1s(1);
			 }
		 }
		 //if (Bzd==0)
		 // reo("Bzd\r\n");
		 //C_Sndhex("Is C\r\n",6);
		 
		 IWDG_ReloadCounter();
		 if (QstCdurl)
	   {
			   QstCdurl=Mhan;
				 QstCBuf[QstCIvk]=0x00; 
			 
			   if (QstCIvk<4)
           QstCBuf[3]=0xC0; 
			   qvwcmd(*(zms *)QstCBuf);
			   
			//C_Sndhex("Is C\r\n",6);
			 
			   memset(QstCBuf,0x00,sizeof(QstCBuf));
         QstCIvk=0;
		 }	
		 
		 if (QstBdurl)
	   {
			   QstBdurl=Mhan;
				 QstBBuf[QstBIvk]=0x00; 
			 
			   if (QstBIvk<4)
			     QstBBuf[3]=0xB0; 
			   qvwcmd(*(zms *)QstBBuf);
			 
				//B_Sndhex("Is B\r\n",6);
			 
			   memset(QstBBuf,0x00,sizeof(QstBBuf));
         QstBIvk=0;
		 }	
		 		 
		 
		 
		 
		 
		 if (Qstdsv > (5*60))
		 {
			 Qstdsv=0;
		 }
     if (Qstdurl)
	   {
			   Qstdurl=Mhan;
				 Qstbuf[Qstivk]=0x00; 
			 
			   if (Qstivk==PAGSIZ)
				 {
					 flash_write2(DRLSUTDMN,Qstbuf,PAGSIZ);
				 }else
				 {
			      if (Qstivk<4)
			        Qstbuf[3]=0xA0; 
			      qvwcmd(*(zms *)Qstbuf);
			   }
//			   if ((Qstbuf[0]==0x22) && (Qstbuf[Qstivk-1]==0x22))  //ASCII ""
//				 {
//					 for (t=1;t<(Qstivk-1);t+=2)
//					 {
////						 //SendOneByte(0x55);
////						 
////						 tmp=Qstbuf[t];
////					//SendOneByte(tmp);
////						 tmp=ASCII2Hex(tmp);				
////						 sndiic=(tmp<<4) & 0xF0;
////				  //SendOneByte(sndiic);
////						 
////						 tmp=Qstbuf[t+1];
////					//SendOneByte(tmp);
////						 tmp=ASCII2Hex(tmp);
////					//SendOneByte(tmp);
////						 sndiic+=tmp;
//					sndiic=bcd2hex(&Qstbuf[t]);	
//					SendOneByte(sndiic);
//						 //SendOneByte(0x13);
//						 //SendOneByte(0x10);
//					 }
//				 }else
//			     Qst2Sndhex(Qstbuf,Qstivk);				
			   memset(Qstbuf,0x00,sizeof(Qstbuf));
         Qstivk=0;
		 }
		 
		 if (Qst2durl)
	   {
			   Qst2durl=Mhan;
				 Qst2Buf[Qst2Ivk]=0x00; 
			 
			   if ((*(zms *)Qst2Buf)==TRIEV)
				 {
					 //rbb();
					 Leduwd;
					 serhwo=Han;
					 sertimcnt=0;
				 }
				 else if (serhwo || Cizcitwem || (Bzd==0))
				 {
					 Ledior;
					 Dsv_ms(50);
					 serhwo=Mhan;
			     if (Qst2Ivk<4)
			       Qst2Buf[3]=0xA0; 
			     qvwcmd(*(zms *)Qst2Buf);
			   }
				 
//			   if ((Qst2Buf[0]==0x22) && (Qst2Buf[Qst2Ivk-1]==0x22))  //ASCII ""
//				 {
//					 for (t=1;t<(Qst2Ivk-1);t+=2)
//					 {
////						 tmp=Qst2Buf[t];
////						 tmp=ASCII2Hex(tmp);				
////						 sndiic=(tmp<<4) & 0xF0;
////						 
////						 tmp=Qst2Buf[t+1];
////						 tmp=ASCII2Hex(tmp);
////						 sndiic+=tmp;
//					sndiic=bcd2hex(&Qst2Buf[t]);	 
//					Fcwawciriic(sndiic);
//					 }
//				 }else
//			     QstSndhex(Qst2Buf,Qst2Ivk);				
			   memset(Qst2Buf,0x00,sizeof(Qst2Ivk));
         Qst2Ivk=0;
		 }
		 		 
		 
     if (uoIratuo)
		 {
			 uoIratuo=Mhan;
			 Gaocis=0;	 
		 }
		
	}
}

